package com.yaoxuguang.concurrence.synchronizedpackage.notThisSynchronizedCodeBlock;

import java.util.concurrent.TimeUnit;

/**
 * @author yaoxuguang
 * @Title: yao-interview
 * @Package com.yaoxuguang.concurrence.synchronizedpackage.notThisSynchronizedCodeBlock
 * @Description: 非synchronized(this)的同步代码块
 * @date 2018年12月18日14:08
 */
public class SyncCodeBlock {
    private String usernameParam;
    private String passwordParam;
//    String str = new String();

    public void lock(String username, String passsword) {
        String str1 = new String();
        try {
//            synchronized (this){
//            synchronized (str){
            synchronized (str1) {
                System.out.println("线程名称为：" + Thread.currentThread().getName() +
                        "在" + System.currentTimeMillis() + "进入同步代码块");
                usernameParam = username;
                TimeUnit.SECONDS.sleep(3);
                passwordParam = passsword;
                System.out.println("线程名称为：" + Thread.currentThread().getName() +
                        "在" + System.currentTimeMillis() + "离开同步代码块");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
